package com.taikang.hmp.hbusiness.doctor.hplan.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.frame.customer.bean.CustomerInfo;
import com.taikang.hmp.hbusiness.doctor.hplan.bean.Hdbzdchealthplan;
import com.taikang.hmp.hbusiness.doctor.hplan.bean.Hdbzdchealthplando;
import com.taikang.hmp.hservice.doctor.healthrickevalmodel.bean.HypertensionReport;

public class HealthPlanServiceImpl extends BaseServiceImpl  implements IHealthPlanService {

	public List<Dto> getHealthPlanList(Dto paramDto) {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Map> lists = new ArrayList<Map>();
//		if(paramDto.getAsString("operatedate")!=null){
//			String sdf=paramDto.getAsString("operatedate").toString();
//			map.put("operate_date",sdf);
//		}
		map.put("operate_date",paramDto.getAsString("operatedate"));
		//加一天时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(paramDto.getAsString("operatedate"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	     Calendar   calendar   =   new   GregorianCalendar(); 
	     calendar.setTime(date); 
	     calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
	     date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
	     String d = sdf.format(date);
	     map.put("opd", d);
		if("36c0bd06-2d1a-46ce-985b-fea70071dd03".equals(paramDto.getAsString("uid")) || "".equals(paramDto.getAsString("uid")) || paramDto.getAsString("uid")==null){
			lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListnm");
		}else{
			List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
			String customerid = customerIdList.get(0).getCustomer_id();
			map.put("customerid", customerid);
			List<HypertensionReport> hy = super.getNewDao().queryForList("assess_space.queryjkpg", customerid);
			if(hy.size()>0){
				double bmi = Math
						.round(Double.parseDouble(hy.get(0).getWeight())
								/ Math.pow(Double.parseDouble(hy.get(0)
										.getHeight()) / 100, 2) * 10) / 10.0;
				List<String> tag = new ArrayList<String>();
				if(bmi>=24){
					tag.add("14");
				}
				if(hy.get(0).getDm()==0){
					tag.add("15");
					tag.add("16");
					tag.add("17");
					tag.add("18");
				}
				if(hy.get(0).getHypertension()==0){
					tag.add("19");
					tag.add("20");
					tag.add("21");
					tag.add("22");
				}
				if(hy.get(0).getGxz()==0){
					tag.add("23");
					tag.add("24");
					tag.add("25");
					tag.add("26");
				}
				if(hy.get(0).getTf()==0){
					tag.add("27");
					tag.add("28");
					tag.add("29");
				}
				if(hy.get(0).getScore().contains("5:A")||hy.get(0).getScore().contains("5:B")){
					tag.add("30");
				}
				if(hy.get(0).getScore().contains("6:A")){
					tag.add("31");
				}
				if("1".equals(hy.get(0).getSex())&&(hy.get(0).getScore().contains("8:A")||hy.get(0).getScore().contains("8:B"))
						&&!"0".equals(hy.get(0).getDm())&&!"0".equals(hy.get(0).getHypertension())&&!"0".equals(hy.get(0).getGxz())&&!"0".equals(hy.get(0).getTf())){
					tag.add("32");
				}
				if("0".equals(hy.get(0).getSex())&&(hy.get(0).getScore().contains("8:A")||hy.get(0).getScore().contains("8:B"))
						&&!"0".equals(hy.get(0).getDm())&&!"0".equals(hy.get(0).getHypertension())&&!"0".equals(hy.get(0).getGxz())&&!"0".equals(hy.get(0).getTf())){
					tag.add("33");
				}
				
				boolean no_drink = false;
				String first_drink = "";
				for (String t : tag) {
					if (t.equals("18") || t.equals("22") || t.equals("26") || t.equals("27")) {
						first_drink = t;
						no_drink = true;
						break;
					}
				}
				
				if (no_drink) {
					for (int i = tag.size() - 1; i >= 0; i --) {
						String t = tag.get(i);
						if ((t.equals("18") || t.equals("22") || t.equals("26") || t.equals("27")) && !t.equals(first_drink)) {
							tag.remove(i);
						}
					}
				}
				
				//HashSet<String> hs = new HashSet<String>();//存储tag的值
//				List<String> str = new ArrayList<String>();
				if(tag.size()>=5){
//					while(str.size()<=5){
//						int index=(int)(Math.random()*tag.size()); 
//						str.add(tag.get(index));
//					}
					for(int i=1;i<=5;i++){
						map.put("tag"+i, tag.get(i-1));
					}
					lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListByReport1", map);
				}else{
					if(tag.size()==0){
						lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListByReport2", map);
					}else if(tag.size()==1){
						map.put("tag1", tag.get(0));
						lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListByReport3", map);
					}else if(tag.size()==2){
						map.put("tag1", tag.get(0));
						map.put("tag2", tag.get(1));
						lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListByReport4", map);
					}else if(tag.size()==3){
						map.put("tag1", tag.get(0));
						map.put("tag2", tag.get(1));
						map.put("tag3", tag.get(2));
						lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListByReport5", map);
					}else if(tag.size()==4){
						map.put("tag1", tag.get(0));
						map.put("tag2", tag.get(1));
						map.put("tag3", tag.get(2));
						map.put("tag4", tag.get(3));
						lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListByReport6", map);
					}
//					for(int i=1;i<=tag.size();i++){
//						map.put("tag"+i, tag.get(i-1));
//					}
//					lists=super.getNewDao().queryForList("hplan_space.getHealthPlanListReport2", map);
				}
			}else{
				lists=super.getNewDao().queryForList("hplan_space.getHealthPlanList", map);
			}
			
		}
		List<Dto> result = new ArrayList<Dto>();
		if(lists.size()>0){
//a.plan_id, a.TITLE,a.CONTENT,a.warn,a.bdate,a.repeat, b.STATUS,a.CUSTOMIZE,b.doplan_id  from HD_BZ_DC_HEALTHPLAN  A left join HD_BZ_DC_HEALTHPLANDO B on a.plan_id=b.plan_id where a.CUSTOMER_ID=#customer_id#
			for (int i = 0; i < lists.size(); i++) {
				Map map222=lists.get(i);
				Dto resultMap = new BaseDto();
				resultMap.put("planid",map222.get("plan_id")==null?"":map222.get("plan_id").toString());
				resultMap.put("title", map222.get("title")==null?"":map222.get("title").toString());
				resultMap.put("content",map222.get("content")==null?"":map222.get("content").toString());
				resultMap.put("warn", map222.get("warn")==null?"":map222.get("warn").toString());
				resultMap.put("bdate", map222.get("bdate")==null?"":map222.get("bdate").toString());
				resultMap.put("repeat", map222.get("repeat")==null?"":map222.get("repeat").toString());
				resultMap.put("status", map222.get("status")==null?"":map222.get("status").toString());
				resultMap.put("customize",map222.get("customize")==null?"":map222.get("customize").toString());
				resultMap.put("doplanid", map222.get("doplan_id")==null?"":map222.get("doplan_id").toString());
				resultMap.put("tag", map222.get("tag")==null?"":map222.get("tag").toString());
				result.add(resultMap);
			}
		}
		return result;

	}

	@Override
	public Hdbzdchealthplan addHealthPlan(Dto paramDto) {
		Hdbzdchealthplan hp=new Hdbzdchealthplan();
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String customerid = customerIdList.get(0).getCustomer_id();
		String id=java.util.UUID.randomUUID().toString();
		hp.setContent(paramDto.getAsString("content"));
		hp.setTitle(paramDto.getAsString("title"));
		hp.setRepeat(paramDto.getAsString("repeat"));
		hp.setPlan_id(id);
		hp.setBdate(paramDto.getAsString("bdate"));
		hp.setWarn(paramDto.getAsString("warn"));
		hp.setCustomize("0");
		hp.setCustomer_id(customerid);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
		try {
			hp.setOperate_date(sdf.parse(sdf.format(new Date())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		super.getNewDao().insert("hplan_space.addHealthPlan",hp);
		
		
		Hdbzdchealthplando hpo=new Hdbzdchealthplando();
		hpo.setPlan_id(id);
		hpo.setDoplan_id(java.util.UUID.randomUUID().toString());
		hpo.setDates(sdf1.format(new Date()));
		hpo.setStatus("0");
		//hpo.setCustomer_id(paramDto.getAsString("customerid"));
		hpo.setCustomer_id(customerid);
		
		super.getNewDao().insert("hplan_space.addHealthPlando",hpo);
		return hp;
	}

	@Override
	public void updateHealthPlando(Dto paramDto) {
		Hdbzdchealthplando hpo=new Hdbzdchealthplando();
		String dopalnid = java.util.UUID.randomUUID().toString();
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String customerid = customerIdList.get(0).getCustomer_id();
		hpo.setCustomer_id(customerid);
		hpo.setPlan_id(paramDto.getAsString("planid"));
		hpo.setStatus(paramDto.getAsString("status"));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		hpo.setDates(sdf.format(new Date()));
		if("0".equals(paramDto.getAsString("tag"))){//用户自己添加的计划 完成
			//查询添加的计划执行情况记录在不在
			Hdbzdchealthplando dd = (Hdbzdchealthplando)super.getNewDao().queryForObject("hplan_space.queryHealthPlanDoExist", hpo);
			if(dd!=null){
				super.getNewDao().update("hplan_space.updateHealthPlando",hpo);
			}else{
				hpo.setDoplan_id(dopalnid);
				//添加当天的用户自己完成的计划
				super.getNewDao().insert("hplan_space.insertHealthPlandoSelf", hpo);
			}
		}else{//用户固定的计划
			hpo.setDoplan_id(dopalnid);
			List<Hdbzdchealthplando> plando = super.getNewDao().queryForList("hplan_space.queryHealthPlando", hpo);
			if(plando.size()>0){
				super.getNewDao().delete("hplan_space.delPlando", plando.get(0).getDoplan_id());
			}
			super.getNewDao().insert("hplan_space.addHealthPlando1", hpo);
		}
		
	}

	@Override
	public void deleteHealthPlan(Dto paramDto) {
		Map<String,String> map = new HashMap<String,String>();
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String customerid = customerIdList.get(0).getCustomer_id();
		map.put("planid", paramDto.getAsString("planid"));
		map.put("customerid", customerid);
		super.getNewDao().delete("hplan_space.deletePlan",map);
		super.getNewDao().delete("hplan_space.deletePlando",map);
	}

	@Override
	public void updateHealthPlan(Dto paramDto) {
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String customerid = customerIdList.get(0).getCustomer_id();
		Map<String,String> map = new HashMap<String,String>();
		map.put("planid", paramDto.getAsString("planid"));
		map.put("content", paramDto.getAsString("content"));
		map.put("title", paramDto.getAsString("title"));
//		map.put("repeat", paramDto.getAsString("repeat"));
//		map.put("bdate", paramDto.getAsString("bdate"));
//		map.put("warn", paramDto.getAsString("warn"));
		map.put("customerid", customerid);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("operatedate",sdf.format(new Date()) );
		super.getNewDao().insert("hplan_space.updateHealthPlan",map);
	}

}
